using System;
using System.Collections.Generic;
using System.Linq;

namespace Storage.Core
{
	internal class ReadByTypeResponse : IStorageResponse
	{
		public ReadByTypeResponse(IList<PlainEntity> entities)
		{
			Entities = entities;
		}

		public byte[] Serialize()
		{
			int estimatedSize = sizeof(byte) + sizeof(Int32) + Entities.Sum(entity => entity.EstimatedSerializedSize());
			var serializer = new FastBinarySerializer(estimatedSize);
			serializer.Write(StorageOpCodes.ReadByType);
			PlainEntity.SerializeList(Entities, serializer);
			return serializer.Buffer;
		}

		public static ReadByTypeResponse Deserialize(byte[] content)
		{
			var deserializer = new FastBinaryDeserializer(content, 1);
			return new ReadByTypeResponse(PlainEntity.DeserializeList(deserializer));
		}

		public IList<PlainEntity> Entities { get; private set; }
	}
}